package com.prd.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.dto.SysPositionDescribeDTO;
import com.prd.mapper.SysPositionDescribeMapper;
import com.prd.model.LoginUser;
import com.prd.model.SysPositionDescribeDO;
import com.prd.service.SysPositionDescribeService;
import com.prd.util.CommonUtil;
import com.prd.util.JsonData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 职务说明书 服务实现类
 * </p>
 *
 * @author lizhaoyi
 * @since 2024-01-28
 */
@Service
public class SysPositionDescribeServiceImpl extends ServiceImpl<SysPositionDescribeMapper, SysPositionDescribeDO> implements SysPositionDescribeService {


    @Autowired
    private SysPositionDescribeMapper describeMapper;

    /**
     * 新建职务说明书
     * @param describeDTO
     * @return
     */
    @Override
    public JsonData add(SysPositionDescribeDTO describeDTO) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        if (loginUser == null) {
            describeDTO.setCreateBy("system");
        }else {
            describeDTO.setCreateBy(loginUser.getName());
        }
        describeDTO.setCreatedTime(new Date());

        int insert = describeMapper.insert(CommonUtil.dtoTransfer(describeDTO, SysPositionDescribeDO.class));

        return insert> 0 ? JsonData.buildSuccess() : JsonData.buildError("新建职务说明书,操作异常");
    }

    /**
     * 查看职务说明书详情
     * @param id
     * @return
     */
    @Override
    public JsonData detail(Long id) {
        return JsonData.buildSuccess(describeMapper.selectById(id));
    }

    /**
     * 修改职务说明书
     * @param describeDTO
     * @return
     */
    @Override
    public JsonData edit(SysPositionDescribeDTO describeDTO) {
        SysPositionDescribeDO describeDO = describeMapper.selectById(describeDTO.getId());
        BeanUtils.copyProperties(describeDTO,describeDO);
        int conunt = describeMapper.updateById(describeDO);
        return conunt > 0 ? JsonData.buildSuccess() : JsonData.buildError("修改职务说明书 操作异常");
    }
}
